#include "stdafx.h"
#include "render/Surface.h"
#include "SDL_image.h"

namespace Render
{

Surface Load(const char* filename) 
{
	SDL_Surface* Surf_Temp = NULL;
	SDL_Surface* Surf_Return = NULL;
	
	if((Surf_Temp = IMG_Load(filename)) == NULL) 
	{        
		return Surface();
	}     
	
	SDL_SetColorKey(Surf_Temp, SDL_SRCCOLORKEY, 0xff00ffff);
	Surf_Return = SDL_DisplayFormatAlpha(Surf_Temp);
	SDL_SetAlpha(Surf_Return, SDL_SRCALPHA, 255);
	SDL_FreeSurface(Surf_Temp); 
	return Surface((Surface_Raw*) Surf_Return);
}

Surface CreateSurface(int width, int height)
{
	U32 amask = 0xff000000;
	U32 rmask = 0x00ff0000;
    U32 gmask = 0x0000ff00;
    U32 bmask = 0x000000ff;

	SDL_Surface* pSurface = SDL_CreateRGBSurface(SDL_SRCALPHA, width, height, 32, rmask, gmask, bmask, amask);
	SDL_SetAlpha(pSurface, SDL_SRCALPHA, 255);

	return Surface((Surface_Raw*) pSurface);
}

int GetWidth(const SurfacePtr surface)
{
	return ((SDL_Surface*) surface)->w;
}

int GetHeight(const SurfacePtr surface)
{
	return ((SDL_Surface*) surface)->h;
}

void ReferenceCountedSurface::FreeSurface()
{
	SDL_FreeSurface((SDL_Surface*) pSurface);
}

}